package com.cn.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import org.apache.struts2.ServletActionContext;

import com.cn.domain.Customer;
import com.cn.service.CustomerService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LoginAction extends ActionSupport implements ModelDriven<Customer>{
    
	private Customer customer = new Customer();
    private CustomerService customerService;
	private String result;

	public Customer getModel() {
		return customer;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String customerLogin() {

		return null;
	}

	/*
	 * 注册页面
	 */
	public String customerRegist() {
		return "registpage";
	}

	/*
	 * ajax校验用户名不能重复
	 */
	@SuppressWarnings("unused")
	public String ajaxLoginUsername()throws Exception {

		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String loginusername = request.getParameter("name");
		Customer cust = customerService.getLoginUsername(loginusername);
	    String data=null;
		if(cust!=null){
		data="{\"str\":\"100\"}";

		}
		else{
			data="{\"str\":\"200\"}";
		}
       response.getWriter().print(data);
	 
	   return  null;
	
	}
/*
 * 注册用户信息
 */
   public String registCustomer(){
	customer.setRole("1");
	  customerService.addCustomer(customer);
	 
	  return "logincustomer";
  }
   /*
    * 登陆页面
    */
   public String loginpage(){
	   return "loginpage";
   }
   /*
    * 前台的登陆的业务逻辑
    */
   public String loginCustomer(){
	  //页面接受用户名和密码 然后进行查询
	  Customer c=  customerService.getLoginCustomer(customer.getLoginUsername(),customer.getPassword());
      if(c!=null){
      String loginUsername = c.getLoginUsername();
      ActionContext.getContext().getSession().put("exitcustomer", loginUsername);
      return "indexpage";
	}else{
		this.addActionMessage("用户名或密码不正确");
		return "loginpage";
	}
	 
   }
   /*
    * 注销
    */
   public String loginout(){
	   ActionContext.getContext().getSession().remove("exitcustomer");
	   return "indexpage";
   }
   /*
    * 后台管理员登陆界面
    */
   public String adminlogin(){
	 
	 
	   return "adminpage";
   }
  /*
   * 处理的业务逻辑
*/
   public String getLoginAdmin() {
	   Customer c=  customerService.getLoginAdmin(customer.getLoginUsername(),customer.getPassword());
	   if(c!=null){
		      String loginUsername = c.getLoginUsername();
		      ActionContext.getContext().getSession().put("exitadmincustomer", loginUsername);
		      return "adminindex"; 
			}else{
				this.addActionMessage("用户名或密码不正确");
				return "adminpage";
			}
	   
   }  
   /*
    * 管理员注销
    */
   public String adminLoginout(){
	   ActionContext.getContext().getSession().remove("exitadmincustomer");
	   return "adminpage";
   }
	
}
